

/**
 * 内容不为空判定
 * @param {Object} value	判断内容
 */
function isNull(value) {
	if(value === null || value === undefined || value === "" || value === '') return true;
	var regu = "^[ ]+$";
	var re = new RegExp(regu);
	return re.test(value);
}


/*----- start : 前端直传服务器文件上传业务 -----*/
function uploadFileToSever(file, callBack) {
	uploadFileToQiniu(file, callBack)
}

function uploadFileToQiniu(file, callBack) {
	getAppUploadToken(function(tokenData) {
		var fileKey = getAppUploadFileName();
        var config = {
                useCdnDomain: true,
                disableStatisticsReport: false,
                retryCount: 6
            };
        var putExtra = {
                fname: "",
                params: {},
                mimeType: ["image/png", "image/jpeg", "image/gif"]
            };
		var observable = qiniu.upload(file, fileKey, tokenData.authtoken, putExtra, config);
		var subscription = observable.subscribe({
			next(res) {},
			error(err) {
				alert("上传失败");
			},
			complete(res) {
				if(!isNull(tokenData.domain)) {
					res['domain'] = tokenData.domain;
				}
				if(!isNull(res.key)) {
					res['halfpath'] = '/' + res.key;
					res['fileurl'] = res.domain + '/' + res.key;
				}
				if(callBack != null)
					callBack(res);
			}
		})
	});
}

function getAppUploadToken(callBack) {
    $.ajax({
        type: "POST",
        url: "/api/outer/upload/getQiniuToken",
        data: {},
        success: function (res) {
			if(callBack != null)
				callBack(res.data);
        },
		error: function(is, msg) {
			alert(msg);
		}
    });
}

function getAppUploadFileName() {
	var now = new Date();
	var nowFormat = now.Format('yyyy/MM/dd/hhmmssS.jpg');
	return "upload/manage/" + nowFormat;
}